package number;

import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class SetNumber extends Component {
    public int[] number = new int[4];
    static ArrayList<Integer> list = new ArrayList<>();
    public SetNumber(){
        try (
                BufferedReader reader = new BufferedReader(new FileReader("Test.txt"))) {
            for (int i = 0; i < 4; i++) {
                String line = reader.readLine();
                if (line == null) {
                    JOptionPane.showMessageDialog(this, "文件内容不足4行", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                int intValue = Integer.parseInt(line);
                number[i] = intValue;
            }

            //要是文件里面还有值，就继续读
            while (reader.ready()) {
                String line = reader.readLine();
                int intValue = Integer.parseInt(line);
                list.add(intValue);
            }
            JOptionPane.showMessageDialog(this, "加载成功", "成功", JOptionPane.INFORMATION_MESSAGE);
        } catch (IOException e) {
            e.printStackTrace();

            JOptionPane.showMessageDialog(this, "加载失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "文件中的值无效", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public int[] getNumber(){
        return number;
    }
    public static ArrayList<Integer> getList(){
        return list;
    }
}
